package com.longtop.leaning.ui;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Item;
import javax.microedition.lcdui.StringItem;
import javax.microedition.lcdui.TextField;

import com.longtop.leaning.dao.UserDaoFactory;
import com.longtop.leaning.dao.UserRMSDao;
import com.longtop.leaning.data.AppData;
import com.longtop.leaning.data.UIData;
import com.longtop.leaning.entity.User;

public class LoginForm extends Form implements CommandListener{

	private Item noteItem = null;
	private static TextField userTextField = null;
	private static TextField pwTextField = null;
	
	public LoginForm() {
		super("登录");
		userTextField = new TextField ("工号：", "", 15, TextField.ANY);
		pwTextField = new TextField ("密码：", "", 15, TextField.PASSWORD);
		this.append (userTextField);
		this.append (pwTextField);
		this.addCommand(UIData.CMD_BACK);
		this.addCommand(UIData.CMD_OK);
		this.setCommandListener(this);
	}

	public void commandAction(Command c, Displayable d) {
		if (c == UIData.CMD_OK) {
			String loginid = userTextField.getString().trim();
			String pw = pwTextField.getString().trim();
			System.out.println("loginid:"+loginid);
			System.out.println("password:"+pw);
			User user = UserDaoFactory.getUserDao().getUser(loginid, pw);
			if(user != null){
				//登录成功处理
				if(AppData.isOnLine){
					UIData.display.setCurrent(new FunctionList());
					UserRMSDao.getInstance().addUser(user);
				}else{
					UIData.display.setCurrent(new MainForm());
				}
			}else{
				//登录失败处理
				if(noteItem == null){
					noteItem = new StringItem ("工号或者密码错误！请重新输入！","");
					this.insert(0, noteItem);
				}
			}
		}else if(c == UIData.CMD_BACK){
			UIData.display.setCurrent(new ModeList());
		}
	}
	
}
